package com.example.zhunongzonghe.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zhunongzonghe.beans.ZnExchangeCommentBean;
import com.example.zhunongzonghe.common.Result;
import com.example.zhunongzonghe.entity.ZnExchangeComment;
import com.example.zhunongzonghe.service.IZnExchangeCommentService;
import com.example.zhunongzonghe.service.IZnUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;


@RestController
@RequestMapping("/znExchangeComment")
public class ZnExchangeCommentController {
    @Autowired
    private IZnExchangeCommentService znExchangeCommentService;
    @Autowired
    private IZnUsersService iZnUsersService;

    /**
     * 后台全部帖子数据
     * @param currentPage
     * @param pageNum
     * @param SearchInfo
     * @return
     */
    @GetMapping("/CommentAll")
    public Result CommentAll(@RequestParam("currentPage") Integer currentPage,
                                   @RequestParam("pageSize") Integer pageNum,
                                   @RequestParam("input") Object SearchInfo){
        Page<ZnExchangeCommentBean> commentBeanPage = znExchangeCommentService.CommentAll(currentPage,pageNum,SearchInfo);
        return new Result<>(true,"查询成功",200,commentBeanPage);
    }

    /**
     * 后台，删除帖子
     * @param commentId
     * @return
     */
    @DeleteMapping("/DeleteComments/{commentId}")
    public Result<ZnExchangeComment> DeleteComments(@PathVariable Integer commentId){
        znExchangeCommentService.removeById(commentId);
        return new Result<>(true,"删除成功",200);
    }

    /**
     * 前台查询评论
     * @param id
     * @param currentPage
     * @param pageNum
     * @return
     */
    @GetMapping("/queryComments")
    public Result queryComments (@RequestParam("postId") Integer id ,@RequestParam("currentPage") Integer currentPage,
                      @RequestParam("pageSize") Integer pageNum  ){
        if (id == null) return new Result(false,"数据异常，请联系管理员",400);
        Page<ZnExchangeCommentBean> page = znExchangeCommentService.CommentList(id,currentPage,pageNum);
        return new Result(true,"成功",200,page);
    }

    /**
     * 用户发表根评论
     * @param commentBean
     * @param principal
     * @return
     */
    @PostMapping("/postAddComment")
    public Result postAddComment(@RequestBody ZnExchangeCommentBean commentBean, Principal principal){
        System.out.println(commentBean);
        znExchangeCommentService.AddRootComment(commentBean,principal.getName());
        return new Result(true,"发布成功",200);
    }

    @PostMapping("/postWriteBack")
    public Result postWriteBack(@RequestBody ZnExchangeCommentBean commentBean, Principal principal){
        System.out.println(commentBean);
        znExchangeCommentService.AddSonComment(commentBean, principal.getName());
        return new Result(true,"发布成功",200);
    }
}